<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>

<META NAME="author" CONTENT="Dawood Ajaz">
<META NAME="description" CONTENT="Debt Roll-down Calculator">
<META NAME="keywords" CONTENT="debt, rolldown, java, calculator, roll-down, payoff">

<html>
<head>
    <title>Debt Eliminator Service</title>
    <link rel="stylesheet" type="text/css"
          href="<c:url value='/pages/css/styles.css'/>"/>
</head>
<body>
<html:form styleId="DebtInfoForm" action="/paymentPlan.do?reqCode=generatePaymentPlan" method="post">

  <table class="invisTable">
   <tr>
    <td>
        <table width="600" border="1" cellpadding="0" cellspacing="0" class="tableborder">
            <tbody>
            <tr>
                <th height="29" colspan="6">Current Debt Information</th>
            </tr>
            <tr class="headerRow">
                <th width="106" height="50">&nbsp;Debt Type</th>
                <th width="160">&nbsp;Creditor</th>
                <th width="78">&nbsp;Balance</th>
                <th width="73">&nbsp;Required<br>&nbsp;Minimum<br>&nbsp;Payment</th>
                <th width="83">&nbsp;Interest <br>&nbsp;Rate</th>
                <th width="83">&nbsp;Time to Payoff (months)</th>
            </tr>
            <c:forEach begin="0" end="9" varStatus="i">
                <tr>
                    <td>&nbsp;Credit Card</td>
                    <td><html:text property="creditor[${i.index}]" /></td>
                    <td><html:text property="balance[${i.index}]" size="10" onkeypress="return isNumberKey(event)" /></td>
                    <td><html:text property="minimumPayment[${i.index}]" size="10" onkeypress="return isNumberKey(event)" /></td>
                    <%-- <td><html:text property="actualPayment[${i.index}]" size=\"10" /></td> --%>
                    <td><html:text property="rate[${i.index}]" size="10" onkeypress="return isNumberKey(event)"/></td>
                    <td><html:text property="monthsToPayOff[${i.index}]" size="10" readonly="true" /></td>
                </tr>
            </c:forEach>
            <tr>
                <th height="25" colspan="6">Other Information</th>
            </tr>
            <tr>
                <td colspan="2">&nbsp;Extra Monthly Payment</td>
                <td colspan="4">
                    <html:text property="extraPayment" onkeypress="return isNumberKey(event)"/></td>
            </tr>
            <tr align="center">
                <td colspan="6"><html:submit value="Submit"/>
                    &nbsp;&nbsp;&nbsp;
                    <c:url var="clearLink" value="/paymentPlan.do">
                        <c:param name="reqCode" value="clearSession"/>
                    </c:url>
                    <input type="button" name="clear" onclick="onclear('${clearLink}');" value="Clear Input" />
                </td>
            </tr>
        </table>
    </td>
    <c:if test="${! empty results}">
        <td>&nbsp;&nbsp;</td>
        <td>
            <h1 align="center">Results Summary</h1><br/>
            <%-- display summary --%>
            <c:set var="actualPlan" value="${results.paymentPlans[0]}"/>
            <c:set var="rollDownPlan" value="${results.paymentPlans[1]}"/>
            <table border="1" width="600" cellpadding="0" cellspacing="0" class="tableborder">
              <tbody>
                <tr class="headerRow">
                    <th height="38">&nbsp;&nbsp;</th>
                    <th>&nbsp;Current Payment Plan&nbsp;</th>
                    <th>&nbsp;Debt-Eliminator Plan&nbsp;</th>
                    <th>&nbsp;Your Savings&nbsp;</th>
                </tr>
                <tr class="alternate">
                    <td>Monthly Debt Payments&nbsp;</td>
                    <td align="right">
                        <fmt:formatNumber value="${actualPlan.totalMonthlyPayment}"
                                          pattern="$#,###,##0.00"/> &nbsp;
                    </td>
                    <td align="right">
                        <fmt:formatNumber value="${rollDownPlan.totalMonthlyPayment}"
                                          pattern="$#,###,##0.00"/> &nbsp;
                    </td>
                    <td align="right">
                        <fmt:formatNumber value="${rollDownPlan.totalMonthlyPayment - actualPlan.totalMonthlyPayment}"
                                          pattern="$#,###,##0.00"/> &nbsp;
                    </td>
                </tr>
                <tr>
                    <td>Total Payments</td>
                    <td align="right">
                        <fmt:formatNumber value="${actualPlan.totalPayment}"
                                          pattern="$#,###,##0.00"/> &nbsp;
                    </td>
                    <td align="right">
                        <fmt:formatNumber value="${rollDownPlan.totalPayment}"
                                          pattern="$#,###,##0.00"/> &nbsp;
                    </td>
                    <td align="right">
                        <fmt:formatNumber value="${actualPlan.totalPayment - rollDownPlan.totalPayment}"
                                          pattern="$#,###,##0.00"/> &nbsp;
                    </td>
                </tr>
                <tr class="alternate">
                    <td>Total Months to Payoff&nbsp;</td>
                    <td align="right">${actualPlan.totalMonthsToPayOff}m
                        (<fmt:formatNumber value="${actualPlan.totalMonthsToPayOff/12}" pattern="##0.0"/>&nbsp;yrs)&nbsp;
                    </td>
                    <td align="right">${rollDownPlan.totalMonthsToPayOff}m
                        (<fmt:formatNumber value="${rollDownPlan.totalMonthsToPayOff/12}" pattern="##0.0"/>&nbsp;yrs)&nbsp;
                    </td>
                    <td align="right">${actualPlan.totalMonthsToPayOff - rollDownPlan.totalMonthsToPayOff}m
                        (<fmt:formatNumber value="${(actualPlan.totalMonthsToPayOff - rollDownPlan.totalMonthsToPayOff)/12}"
                                          pattern="##0.0"/>&nbsp;yrs)&nbsp;
                    </td>
                </tr>
              </tbody>  
            </table>
            <%-- display chart - AWT,SWING Api not supported by Google App Engine
            <c:url var="chartImg" value="/paymentPlan.do">
                <c:param name="reqCode" value="createChart"/>
            </c:url>
            <br/>
            <a name="results"/>
            <IMG src="${chartImg}" border="1"  alt="Chart image" />
            --%>
        </td>
    </c:if>
   </tr>
  </table>

<c:if test="${! empty results}">

    <hr/>

    <h1> Debt Eliminator Payment Results</h1>
    <p>(Payments highlighted in red indicate your fastest payoff)</p>

    <c:set var="rollDownPlan" value="${results.paymentPlans[1]}"/>

    <c:forEach var="paymentSchedule" items="${rollDownPlan.paymentSchedules}" varStatus="i">
        <a name="creditor${i.index+1}"/>
        <table width="800" border="1" cellpadding="0" cellspacing="0" class="tableborder">
            <tbody>
            <tr class="headerRow">
                <th height="40">Creditor&nbsp;</th>
                <th align="left">&nbsp;Month</th>
                <th align="right">Beginning&nbsp;<br>Balance&nbsp;</th>
                <th align="right">Interest&nbsp;</th>
                <th align="right">Payment&nbsp;</th>
                <th align="right">Ending&nbsp;<br>Balance&nbsp;</th>
                <th align="center">Roll-over&nbsp;</th>
            </tr>

            <c:forEach var="scheduleItem" items="${paymentSchedule.paymentScheduleItems}" varStatus="j">

            <c:set var="trClass" value="td"/>
            <c:if test="${j.index % 2 == 0}">
                <c:set var="trClass" value="alternate"/>
            </c:if>

            <!-- set rollover var to check change in rollover -->
            <c:if test="${i.index > 0 and j.index > 0}">
                <c:if test="${scheduleItem.rollOver}">
                    <c:set var="trClass" value="rollover"/>
                </c:if>
            </c:if>

            <tr class="${trClass}">
                <td>&nbsp;${scheduleItem.creditorName}</td>
                <td align="left">&nbsp;<fmt:formatDate value="${scheduleItem.month}" pattern="MM-yyyy"/></td>
                <td align="right">
                    <fmt:formatNumber value="${scheduleItem.beginningBalance}"
                                                    pattern="$#,###,##0"/> &nbsp;</td>
                <td align="right"><fmt:formatNumber value="${scheduleItem.interest}"
                                                    pattern="$#,###,##0"/>&nbsp;</td>
                <td align="right"><fmt:formatNumber value="${scheduleItem.payment}"
                                                    pattern="$#,###,##0"/>&nbsp;</td>
                <td align="right"><fmt:formatNumber value="${scheduleItem.endingBalance}"
                                                    pattern="$#,###,##0"/>&nbsp;</td>
                <td align="center">
                    <c:if test="${scheduleItem.rollOver}">yes&nbsp;</c:if>
                    <c:if test="${!scheduleItem.rollOver}">no&nbsp;</c:if>
                </td>
            </tr>
            </c:forEach>
            <tr>
                <td colspan="7">&nbsp;</td>
            </tr>
        </table>
        <br>
    </c:forEach>
    <script type="text/javascript">
        document.location = "#results";

        function onclear(url) {
            var form = document.getElementsByName('DebtInfoForm')[0];
            if (form != null) {
                form.action = url;
                form.submit();
    }
        }
    </script>
</c:if>
</html:form>
</body>
</html>

<script language=Javascript>
<!--
	function isNumberKey(evt) {
   		var	charCode = (evt.which) ? evt.which : event.keyCode
        if (charCode > 31 && (charCode < 48 || charCode > 57)) {
        return false;
		}
		return true;
	}
//-->
</script>